package org.csu.mypetstore.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.mypetstore.common.CommonResponse;
import org.csu.mypetstore.domain.BO.ItemBO;
import org.csu.mypetstore.domain.PO.CartItem;
import org.csu.mypetstore.domain.PO.Inventory;
import org.csu.mypetstore.domain.PO.Item;
import org.csu.mypetstore.mapper.InventoryMapper;
import org.csu.mypetstore.service.AccountService;
import org.csu.mypetstore.service.CartItemService;
import org.csu.mypetstore.service.CatalogService;
import org.csu.mypetstore.service.Impl.CatalogServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Iterator;
import java.util.List;

@Controller
@CrossOrigin(origins = "*",allowCredentials="true",allowedHeaders = "",methods = {})
@SessionAttributes(value = {"cartItemList", "subTotal", "accountVO"})
public class CartController {

    private String workingItemId;
    @Autowired
    private CatalogServiceImpl catalogService;
    @Autowired
    private CartItemService cartItemService;
    @Autowired
    private InventoryMapper inventoryMapper;
    private QueryWrapper<Inventory> inventoryQueryWrapper = new QueryWrapper<>();
    @Autowired
    private AccountService accountVOService;

    @PostMapping("/accounts/{username}/cartItems")
    @ResponseBody
    public CommonResponse<CartItem> addItemToCart(@PathVariable("username") String username, String itemId){
        Item item = new Item();
        if(!cartItemService.containItemId(username, itemId)){//如果不存在就添加一个,存在就不管了
            boolean isInStock = catalogService.isItemInStock(itemId);
            item = catalogService.getItemFromItemBO(catalogService.getItem(itemId));
            cartItemService.addItem(username, item, isInStock);
        }
        //TODO 添加已经在购物车的商品要加一，但又要防止用户刷新错误的加一
        return CommonResponse.createForSuccessMessage("添加成功");
    }

    @GetMapping("/accounts/{username}/cartItems")
    @ResponseBody
    public CommonResponse<List<CartItem>> getCartItemList(@PathVariable("username") String username){
        return CommonResponse.createForSuccess(cartItemService.getCartItemList(username), "查询成功");
    }

    @ResponseBody
    @DeleteMapping("/accounts/{username}/cartItems/{itemId}")
    public CommonResponse<String> removeCartItem(@PathVariable("itemId") String itemId,
                                                 @PathVariable("username") String username){
        cartItemService.removeCartItemsByItemId(username, itemId);//将货物移出购物车
        return CommonResponse.createForSuccess("删除cartItem成功");
    }

    @ResponseBody
    @PutMapping("/accounts/{username}/cartItems/{itemId}")
    public CommonResponse<CartItem> updateCart(@PathVariable("itemId") String itemId,
                                               @PathVariable("username") String username,
                                               @RequestBody CartItem cartItem) {
        ItemBO itemBO = catalogService.getItem(cartItem.getItemId());
        Inventory inventory = new Inventory();
        inventoryQueryWrapper.eq("itemId", itemId);
        inventory = inventoryMapper.selectOne(inventoryQueryWrapper);
        int quantity_num = cartItem.getQuantity();//得到修改的值
        if (BigDecimal.valueOf(quantity_num).compareTo(inventory.getQuantity()) < 0)//如果存货不足就直接返回
            return CommonResponse.createForError(itemBO.getItemId() + "存货不足");
        cartItemService.setQuantityByItemId(username, itemId, quantity_num);//如果存货充足就设置数量
        return CommonResponse.createForSuccessMessage("修改成功");
    }
}
